package managedbean.tourist.temp;

import entity.CuisinePrefEntity;
import entity.InterestPrefEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.ItinerarySessionBean;
import session.stateless.SystemMessagesBean;
import session.stateless.TouristAccountSessionBean;
import util.entityhelper.TouristAccountState;
import util.enumeration.CuisineType;
import util.enumeration.Interest;
import util.generator.TouristMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class TouristUpdatePersonalPrefsManagedBean implements Serializable {
    @EJB
    private TouristAccountSessionBean touristAccountMgr;
    @EJB
    private ItinerarySessionBean itineraryMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private TouristAccountState touristAcctState;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    private PageRedirectorBean redirect;
    
    // fields passed from session
    private Long touristAccountId;
    private String preferredLocale;
    private String itineraryType;
    
    // other fields from database (+ display)
    private List<CuisineType> cuisineTypeList;
    private List<Interest> interestList;
    
    // display only
    private MenuModel touristMainMenuModel;
    private MenuModel touristNavMenuModel;
    
    
    public TouristUpdatePersonalPrefsManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("touristAccountId")) {
            touristAccountId = (Long) ctx.getExternalContext().getSessionMap().get("touristAccountId");
            // put back the session IDs taken
            ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountId);
            
//            if (ctx.getExternalContext().getSessionMap().containsKey("itineraryType")) {
//                itineraryType = (String) ctx.getExternalContext().getSessionMap().get("itineraryType");
//                // put back the session IDs taken
//                ctx.getExternalContext().getSessionMap().put("itineraryType", itineraryType);
//            }
        }
        else {
            // redirect back to login
            redirect = new PageRedirectorBean();
            redirect.goTouristLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */    
    }
    
    public void initView(PhaseEvent event) {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        try {
            if (cuisineTypeList == null) {
                this.cuisineTypeList = new ArrayList<CuisineType>();
                
                List<CuisinePrefEntity> cuisineTypes = touristAcctState.getTourist().getPersonalPref().getCuisinePrefs();
                for (int i = 0; i < cuisineTypes.size(); i++) {
                    cuisineTypeList.add(cuisineTypes.get(i).getCuisineType());
                }
            }
            
            if (interestList == null) {
                this.interestList = new ArrayList<Interest>();
                
                List<InterestPrefEntity> interests = touristAcctState.getTourist().getPersonalPref().getInterestPrefs();
                for (int i = 0; i < interests.size(); i++) {
                    //arraylist add the values
                    interestList.add(interests.get(i).getInterest());
                }
            }
            
            // main menu 
            TouristMenuGenerator menuGeneratorMain = new TouristMenuGenerator();
            touristMainMenuModel = menuGeneratorMain.generateTouristMainMenu();
            // nav menu
            TouristMenuGenerator menuGeneratorNav = new TouristMenuGenerator();
            touristNavMenuModel = menuGeneratorNav.generateTouristNavMenu();
        }
        catch (Exception ex) {
            ex.printStackTrace();
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }
    
    public void updateTouristPreferences(ActionEvent event) {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        try {
            touristAccountMgr.setSelectedLocale(preferredLocale);
            touristAcctState = touristAccountMgr.getTouristAccountByID(touristAccountId);
            
            List<CuisinePrefEntity> newCuisinePrefList = new ArrayList<CuisinePrefEntity>();
            for (CuisineType c : cuisineTypeList) {
                CuisinePrefEntity newCuisinePref = new CuisinePrefEntity();
                newCuisinePref.setCuisineType(c);
                newCuisinePrefList.add(newCuisinePref);
            }
            
            List<InterestPrefEntity> newInterestPrefList = new ArrayList<InterestPrefEntity>();
            for (Interest i : interestList) {
                InterestPrefEntity newInterestPref = new InterestPrefEntity();
                newInterestPref.setInterest(i);
                newInterestPrefList.add(newInterestPref);
            }
            
            touristAccountMgr.updateTouristPersonalPrefs(touristAcctState.getTourist().getId(), newCuisinePrefList, newInterestPrefList);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public List<CuisineType> getCuisineTypeList() {
        return cuisineTypeList;
    }

    public void setCuisineTypeList(List<CuisineType> cuisineTypeList) {
        this.cuisineTypeList = cuisineTypeList;
    }

    public List<Interest> getInterestList() {
        return interestList;
    }

    public void setInterestList(List<Interest> interestList) {
        this.interestList = interestList;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public PageRedirectorBean getRedirect() {
        return redirect;
    }

    public void setRedirect(PageRedirectorBean redirect) {
        this.redirect = redirect;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public Long getTouristAccountId() {
        return touristAccountId;
    }

    public void setTouristAccountId(Long touristAccountId) {
        this.touristAccountId = touristAccountId;
    }

    public TouristAccountSessionBean getTouristAccountMgr() {
        return touristAccountMgr;
    }

    public void setTouristAccountMgr(TouristAccountSessionBean touristAccountMgr) {
        this.touristAccountMgr = touristAccountMgr;
    }

    public TouristAccountState getTouristAcctState() {
        return touristAcctState;
    }

    public void setTouristAcctState(TouristAccountState touristAcctState) {
        this.touristAcctState = touristAcctState;
    }

    public MenuModel getTouristMainMenuModel() {
        return touristMainMenuModel;
    }

    public void setTouristMainMenuModel(MenuModel touristMainMenuModel) {
        this.touristMainMenuModel = touristMainMenuModel;
    }

    public MenuModel getTouristNavMenuModel() {
        return touristNavMenuModel;
    }

    public void setTouristNavMenuModel(MenuModel touristNavMenuModel) {
        this.touristNavMenuModel = touristNavMenuModel;
    }

    public ItinerarySessionBean getItineraryMgr() {
        return itineraryMgr;
    }

    public void setItineraryMgr(ItinerarySessionBean itineraryMgr) {
        this.itineraryMgr = itineraryMgr;
    }

    public String getItineraryType() {
        return itineraryType;
    }

    public void setItineraryType(String itineraryType) {
        this.itineraryType = itineraryType;
    }
}